package com.baibei.ebec.home.newIndex;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.model.ProductInfo;
import com.baibei.model.QuotationProductInfo;
import com.baibei.module.ViewUtil;
import com.blankj.utilcode.utils.SizeUtils;
import com.blankj.utilcode.utils.Utils;
import com.baibei.ebec.home.index.listener.OnSelectListener;
import com.rae.swift.Rx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ChenZhenGuang on 2018/5/15/007.
 *
 * @email chenzhenguang@landsone.com
 */

public class CommodityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private OnSelectListener<ProductInfo> mListener;
    private List<QuotationProductInfo> mQuotations;
    private int clickPosition = 0; // 当前点击的
    private boolean isrefresh = true; // 是否刷新


    /**
     * set listener
     *
     * @param listener listener
     */
    void setOnItemSelectListener(OnSelectListener<ProductInfo> listener) {
        mListener = listener;
    }


    void setQuotations(List<QuotationProductInfo> data) {
        clickPosition = 0;
        mQuotations = data;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_commodity, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        final QuotationProductInfo quotation = mQuotations.get(position);
        double scope = quotation.getScope();
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.tvName.setText(quotation.getQuoteName());
        holder.tvCurrentPrice.setText(Rx.formatPrice(quotation.getLastPrice()));
        holder.tvCurrentPrice.setTextColor(ResourcesCompat.getColor(Utils.getContext().getResources(), getRiseColorResource(scope), null));

        if (mQuotations.size() > 2) {
            ViewUtil.setWidth(holder.LinRoot, (int) (ViewUtil.getScreenWidth(Utils.getContext()) / 3));
        } else {
            ViewUtil.setWidth(holder.LinRoot, (int) (ViewUtil.getScreenWidth(Utils.getContext()) / 2));
        }
        holder.LinRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position != clickPosition) {
                    isrefresh = false;
                    notifyDataSetChanged();
                }
                clickPosition = position;
                if (mListener != null) {
                    mListener.onSelectListener(quotation, position);
                }
            }
        });
        if (position == 0) {
            holder.verticalLine.setVisibility(View.GONE);
        } else {
            holder.verticalLine.setVisibility(View.VISIBLE);
        }

        if (position == clickPosition) {
            holder.viewLine.setVisibility(View.GONE);
            holder.tvName.getPaint().setFakeBoldText(true);
            holder.tvCurrentPrice.getPaint().setFakeBoldText(true);
            holder.tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            holder.tvCurrentPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        } else {
            holder.viewLine.setVisibility(View.VISIBLE);
            holder.tvName.getPaint().setFakeBoldText(false);
            holder.tvCurrentPrice.getPaint().setFakeBoldText(false);
            holder.tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            holder.tvCurrentPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        }

        if (isrefresh) {
            //缩放
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.tvCurrentPrice, "scaleY", 0, 1.3f, 1);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.tvCurrentPrice, "scaleX", 0, 1.3f, 1);
            animatorSet.play(scaleX).with(scaleY);
            animatorSet.setDuration(1000).start();
        }
        if (position == getItemCount() - 1) {
            isrefresh = true;
        }
    }

    @Override
    public int getItemCount() {
        return mQuotations == null ? 0 : mQuotations.size();
    }

    /**
     * 更新行情数据
     *
     * @param quotationProductInfo quotationProductInfo
     */
    void update(QuotationProductInfo quotationProductInfo) {

        for (int i = 0; mQuotations.size() > i; i++) {
            if (mQuotations.get(i).getProductId().equals(quotationProductInfo.getProductId())) {
                mQuotations.set(i, quotationProductInfo);
                notifyItemChanged(i);
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_current_price)
        TextView tvCurrentPrice;
        @BindView(R2.id.lin_root)
        LinearLayout LinRoot;
        @BindView(R2.id.vertical_line)
        View verticalLine;
        @BindView(R2.id.view_line)
        View viewLine;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ItemDecoration extends RecyclerView.ItemDecoration {
        Paint dividerPaint;
        int mydevider;

        public ItemDecoration() {
            dividerPaint = new Paint();
            //设置分割线颜色
            dividerPaint.setColor(Utils.getContext().getResources().getColor(R.color.result_view));
            //设置分割线宽度
            mydevider = SizeUtils.dp2px(5);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = mydevider;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int childCount = parent.getChildCount();
            int right = parent.getPaddingRight();
            int left = parent.getPaddingLeft() + mydevider;

            for (int i = 0; i < childCount - 1; i++) {
                View view = parent.getChildAt(i);
                float top = view.getTop() - SizeUtils.dp2px(6);
                float bottom = view.getBottom() - SizeUtils.dp2px(6);
                c.drawRect(left, top, right, bottom, dividerPaint);
            }
        }
    }

    /**
     * 获取颜色
     *
     * @param d d
     * @return 颜色资源
     */
    private int getRiseColorResource(double d) {
        if (d > 0) {
            return R.color.quotationIncreasingPrimary;
        }
        if (d < 0) {
            return R.color.quotationDecreasingPrimary;
        }
        return R.color.textPrimary;
    }

    private int getTransitionColor(double d) {
        if (d > 0) {
            return R.color.lightRise;
        }
        if (d < 0) {
            return R.color.lightFall;
        }
        return R.color.white;
    }
}
